var api = require('../../config/api.js');
var utils = require('../../utils/util.js')
var app = getApp()
var QRCode = require('../../utils/weapp-qrcode.js')
import rpx2px from '../../utils/rpx2px.js'
var qrcode;
const qrcodeWidth = rpx2px(300)
const W = business.getSystemInfoSync().windowWidth;
const rate = 750.0 / W;
const qrcode_w = 300 / rate;

Page({
  data: {
    "bnrUrl": [{
      "url": "../../images/1.jpg"
    }, {
      "url": "../../images/2.jpg"
    }],
    qrcode_w: qrcode_w,
    role: 'teacher',
    today: '今天是年',
    isHide: false
  },
  onLoad: function (options) {
    let that = this;
    business.getSetting({
      success: function (res) {
        console.log("getSetting:" + JSON.stringify(res));
        if (!res.authSetting['scope.userInfo']) {
          console.log("没有授权");
          that.setData({ isHide: true });
        } else {
          that.setData({ today: '今天是' + utils.formatDate(new Date()) });
          that.businesslogin();
        }
      }
    });
  },
  businesslogin: function () {
    let that = this;
    app.businessLogin().then(res => {
      var openId = res.openId;
      var user = res.user;
      var token = res.token;
      business.clearStorageSync();
      business.setStorageSync('token', token);
      business.setStorageSync('user', user);
      business.setStorageSync('openId', openId);
      
      if (user == null || user == "") {
        business.reLaunch({ url: '../login/login' })
      } else {
        var userType = user.userType;
        app.globalData.user = user;
        var params = new Object();
        params.userId = user.userId;
        that.loadData(params);
        
        if (userType.indexOf("2") > -1 || userType.indexOf("6") > -1) {
          that.setData({ role: 'teacher' });
          app.globalData.role = 'teacher';
        } else if (userType.indexOf("4") > -1) {
          qrcode = new QRCode('canvas', {
            width: qrcode_w,
            height: qrcode_w,
            colorDark: "#1CA4FC",
            colorLight: "white",
            correctLevel: QRCode.CorrectLevel.H,
          });
          qrcode.makeCode(user.mobile);
          that.setData({ role: 'emp' });
        } else if (userType.indexOf("5") > -1) {
          that.setData({ role: 'headmaster' });
          app.globalData.role = 'headmaster';
        } else if (userType.indexOf("7") > -1) {
          that.setData({ role: 'deptadmin' });
          app.globalData.role = 'deptadmin';
        } else {
          business.showModal({
            title: '温馨提示',
            content: '请联系管理员设置',
            showCancel: false
          });
          business.reLaunch({ url: '../login/login' })
        }
      }
    });
  },
  loadData: function (params) {
    let that = this;
    utils.request(api.GetuserInfo, params).then(function (res) {
      var classList = res.data.classList;
      if (classList != null) {
        var array = new Array();
        classList.forEach(function (item) {
          var cls = {};
          cls["classId"] = item.classId;
          cls["className"] = item.className;
          array.push(cls);
        });
        app.globalData.classes = array;
      }
      var schoolList = res.data.schoolList;
      if (schoolList != null) {
        var array = new Array();
        schoolList.forEach(function (item) {
          var cls = {};
          cls["schoolId"] = item.schoolId;
          cls["schoolName"] = item.schoolName;
          array.push(cls);
        });
        app.globalData.schoolList = array;
      }
    })
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      var that = this;
      console.log("用户的信息如下：");
      console.log(e.detail.userInfo);
      that.setData({ isHide: false });
      that.businesslogin();
    } else {
      business.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入系统，请授权之后再进入!',
        showCancel: false,
        confirmText: '返回授权',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击了"返回授权"');
          }
        }
      });
    }
  },
  onShow: function () {
  },
  gobindMobile: function (e) {
    business.navigateTo({ url: '../login/login' })
  }
})